<?php
/**
 * Source code of XmlSerialize class
 * @category puntoengine
 * @package core
 * @subpackage xml
 * @author Juan Benavides Romero <jbalde@gmail.com>
 * @since 0.4.0
 */



/**
 * XmlSerialize is a serializer class for objects
 * @category puntoengine
 * @package core
 * @subpackage xml
 * @author Juan Benavides Romero <jbalde@gmail.com>
 * @since 0.4.0
 */
class XmlSerialize extends Object {
	public static function serialize($mixed) {
		$tag = 'Node';
		
		if(gettype($mixed) == 'array') {
			$tag = 'Array';
		}
		
		return self::process($mixed, $tag);
	}
	
	
	protected static function process($mixed, $tag = 'Node') {
		$xml = null;
		$tag = ucfirst($tag);
		
		switch(gettype($mixed)) {
			case 'array':
				$xml = self::serializeArray($mixed, $tag);
			break;
			case 'string':
				$xml = self::serializeString($mixed, $tag);
			case 'object':
			break;
			default:
				echo gettype($mixed); die;
				throw new NotImplementedException(__METHOD__);
		}
		
		return $xml;
	}
	
	
	protected static function serializeString($string, $tag = 'String') {
		$xml = '<'.$tag.'>';
		$xml .= $string;
		$xml .= '</'.$tag.'>';
		
		return $xml;
	}
	
	
	protected static function serializeArray(array $array, $tag = 'Array') {
		ksort($array, SORT_STRING);
		
		$xml = '<'.$tag.'>';
		
		foreach($array as $key => $value) {
			if(is_numeric($key)) {
				$key = 'Item';
			}

			$xml .= self::process($value, $key);
		}
		
		$xml .= '</'.$tag.'>';
		
		return $xml;
	}
}